<?php
namespace app\index\controller;

use app\index\Controller;


class Boot extends Base
{   
    //是否通过用户还杆的操作
    public function isPassReturnBallArm(){
        $Charge    = model('Charge');
        $Lock      = model('lock');

        $number  = input('number');

        if($user_id = db('lock')->where('number',$number)->value('user_id')){

            $update['is_pass'] = input('state');
            $update['is_pass'] = $update['is_pass'] == 0 ? '-1' : $update['is_pass'];
            $result = db('lock')->where('number',$number)->update($update);
            if($result || $result === 0){
                $data['state'] = 1;
                $data['info'] = '通讯成功！';
                if($update['is_pass'] == 1){
                    //结算
                    $Charge->endBilling($number,$user_id);
                    //还杆
                    $Lock->userReturnBallArm($number);

                }
            }else{
                $data['state'] = 0;
                $data['info']  = '通讯失败';
            }
            return json($data);
        }else{
            echo "error";
        }

        
    }

       //计算用户的消费
    // private function charge(){
    //     //$form_id   = input('form_id');
    //     $Charge    = model('Charge');
    //     //$user_feedback = input('user_feedback');
    //     $data =  $Charge->endBilling($this->user_id,$user_feedback);
    //     //发送微信小程序消息提示
    //     if($data['status'] == 1){
    //         //$this->sendMessageInfo($form_id,$data['order_info']);
    //     }
    //     return json($data);
    // }
    
    // //发送微信小程序通知
    // private function sendMessageInfo($form_id,$order_info){
    //     $return_time = date('Y-m-d H:i',$order_info['end_time']);
    //     $duration    = formatDuration($order_info['duration']);
    //     $into        = "恭喜您用杆成功，本次的消费金额：{$order_info['amount']}元";
    //     //获取用户的open_id
    //     $openid = db('user')->where('id',$this->user_id)->value('openid');
    //     $message_info['touser']           = $openid;
    //     $message_info['template_id']      = 'j-4aHxsLUQV9MPrQnS0gooE00zrmQ11egRzbJElpCIE';
    //     $message_info['page']             = 'usagelog';
    //     $message_info['form_id']          = $form_id;

    //    // $message_info['data']['keyword1'] = array('value'=>$order_info['order_number'],'color'=>"#173177");
    //     $message_info['data']['keyword1'] = array('value'=>$return_time,'color'=>"#173177");
    //     $message_info['data']['keyword2'] = array('value'=>$duration,'color'=>"#173177");
    //     $message_info['data']['keyword3'] = array('value'=>$into ,'color'=>"#173177");
       
    //     $access_token = getAccessToken();
    //     $url    = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";
       
    //     $result = httpRequest($url,'POST',json_encode($message_info));
        
    //     file_put_contents('test.text', $form_id);
    // }
}